using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;


namespace GameWP
{
    public class UpgradeSkillComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Game _game;
        private SpriteBatch _spriteBatch;
        private SpriteFont _textFont;
        private Vector2 touchPosition, pretouchPosition;
        private Button _label;
        private List<Button> _listSkill;
        private List<Button> _listPriceButton;
        private List<int> _listPrice = new List<int>() {500, 2700, 4500, 5600, 9000, 12000 };

        //Initilation
        public UpgradeSkillComponent(Game game, SpriteFont textFont, SpriteFont minitextFont, SpriteBatch spriteBatch)
            : base(game)
        {
            _game = game;
            _textFont = textFont;
            _spriteBatch = spriteBatch;

            //Init Button
            _label = new Button("Lv 1: \n\nLv 6: \n\nLv 10: \n\n", textFont, new Vector2(30, 200), Color.White) { HaveAnimation = false };
            _listSkill = new List<Button>();
            _listPriceButton = new List<Button>();
            _listSkill.Add(new Button(ManagerGame.luffy.ListButtonSkill[0].Texture, new Vector2(140, 180)));   _listPriceButton.Add(new Button("$" + _listPrice[0].ToString(), minitextFont, new Vector2(205, 190),Color.Yellow){HaveAnimation = false});                
            _listSkill.Add(new Button(ManagerGame.luffy.ListButtonSkill[1].Texture, new Vector2(270, 180)));   _listPriceButton.Add(new Button("$" + _listPrice[1].ToString(), minitextFont, new Vector2(335, 190),Color.Yellow){HaveAnimation = false});
            _listSkill.Add(new Button(ManagerGame.luffy.ListButtonSkill[2].Texture, new Vector2(140, 260)));   _listPriceButton.Add(new Button("$" + _listPrice[2].ToString(), minitextFont, new Vector2(205, 270),Color.Yellow){HaveAnimation = false});
            _listSkill.Add(new Button(ManagerGame.luffy.ListButtonSkill[3].Texture, new Vector2(270, 260)));   _listPriceButton.Add(new Button("$" + _listPrice[3].ToString(), minitextFont, new Vector2(335, 270),Color.Yellow){HaveAnimation = false});
            _listSkill.Add(new Button(ManagerGame.luffy.ListButtonSkill[4].Texture, new Vector2(400, 260)));   _listPriceButton.Add(new Button("$" + _listPrice[4].ToString(), minitextFont, new Vector2(465, 270),Color.Yellow){HaveAnimation = false});
            _listSkill.Add(new Button(ManagerGame.luffy.ListButtonSkill[5].Texture, new Vector2(140, 340)));   _listPriceButton.Add(new Button("$" + _listPrice[5].ToString(), minitextFont, new Vector2(205, 350),Color.Yellow){HaveAnimation = false});
            for (int i = 0; i < _listSkill.Count; i++)
                _listSkill[i].Selected += new Button.IsSelected(UpgradeSkillSelected);
            for (int i = 0; i < ManagerGame.luffy.LevelSkill; i++)
            {
                _listSkill[i].ColorTexture = Color.Gray;
                _listSkill[i].HaveAnimation = false;
            }

        }

        void UpgradeSkillSelected(int input)
        {
            if (input == ManagerGame.luffy.LevelSkill)
            {
                bool CanUpgrade = false;
                switch (input)
                {
                    case 0: case 1:
                        if (ManagerGame.luffy.Balance > _listPrice[input])
                            CanUpgrade = true;
                        break;
                    case 2: case 3: case 4:
                        if (ManagerGame.luffy.Balance > _listPrice[input] 
                            && ManagerGame.luffy.Level >= 6)
                            CanUpgrade = true;
                        break;
                    case 5:
                        if (ManagerGame.luffy.Balance > _listPrice[input]
                            && ManagerGame.luffy.Level >= 10)
                            CanUpgrade = true;
                        break;
                }
                if (CanUpgrade)
                {
                    ManagerGame.luffy.LevelSkill++;
                    ManagerGame.luffy.Balance -= _listPrice[input];                    
                }
            }
        }


        public override void Initialize()
        {
            // TODO: Add your initialization code here
                base.Initialize();
        }


        public override void Update(GameTime gameTime)
        {
            for (int i = 0; i < 6; i++)
            {
                _listSkill[i].ColorTexture = Color.White;
                _listPriceButton[i].Text = "$" + _listPrice[i].ToString();
            }
            for (int i = 0; i < ManagerGame.luffy.LevelSkill; i++)
            {
                _listSkill[i].ColorTexture = Color.Gray;
                _listSkill[i].HaveAnimation = false;
            }
            // TODO: Add your update code here    
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                _game.Exit();
            //Key input
            if (TouchPanel.IsGestureAvailable)
            {
                // Read in the Gesture input
                GestureSample gesture = TouchPanel.ReadGesture();
                // Process Gestures
                if (gesture.GestureType == GestureType.Tap)
                {
                    touchPosition = gesture.Position;
                    for (int i = 0; i < _listSkill.Count; i++)
                        _listSkill[i].ButtonIsTouched(touchPosition, i);
                }
            }

            
                base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            _label.Draw(_spriteBatch, gameTime);
            for (int i = 0; i < _listSkill.Count; i++)
                _listSkill[i].Draw(_spriteBatch, gameTime);
            for (int i = ManagerGame.luffy.LevelSkill; i < _listPriceButton.Count; i++)
                _listPriceButton[i].Draw(_spriteBatch, gameTime);
        }
    }
}
